#!/usr/bin/perl
#################################################################
# hg_add_copyright.pl -- add highgo copyright throughout the source tree.
#
# Copyright (c) 2019, HighGo Software Inc
#
# src/tools/hgchange/hg_native_client_utilities.pl
#
#################################################################

use strict;
use warnings;

use File::Find;
use File::Basename;
use Tie::File;


my @lines;

tie @lines, "Tie::File", "src/backend/Makefile";
foreach my $line (@lines)
{
	next if($line !~ m/postgres/);
#	next if($line =~ m/libpostgres/);
	next if($line =~ m/postgresql.conf/);
	next if($line =~ m/postgres.o/);
	next if($line =~ m/postgres.d/);
	$line =~ s/(?<![-b])postgres/hg-postgres/;
	
}
untie @lines;
# twice for some omit
tie @lines, "Tie::File", "src/backend/Makefile";
foreach my $line (@lines)
{
	next if($line !~ m/postgres/);
#	next if($line =~ m/libpostgres/);
	next if($line =~ m/postgresql.conf/);
	next if($line =~ m/postgres.o/);
	next if($line =~ m/postgres.d/);
	$line =~ s/(?<![-b])postgres/hg-postgres/;
	
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/backend/utils/misc/ps_status.c";
foreach my $line (@lines)
{
	next if($line !~ m/postgres:/);
	$line =~ s/(?<![-])postgres:/hg-postgres:/;
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/bin/initdb/initdb.c";
foreach my $line (@lines)
{
	next if($line !~ m/postgres/);
	if($line =~ m/PG_BACKEND_VERSIONSTR/i)
	{
		$line =~ s/(?<![-])postgres/hg-postgres/;
	}
	elsif($line =~ m/The program/i)
	{
		$line =~ s/(?<![-])postgres/hg-postgres/;
	}
	
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/bin/pg_ctl/pg_ctl.c";
foreach my $line (@lines)
{
	next if($line !~ m/postgres/);
	if($line =~ m/PG_BACKEND_VERSIONSTR/i)
	{
		$line =~ s/(?<![-])postgres/hg-postgres/;
	}
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/bin/psql/startup.c";
foreach my $line (@lines)
{
	$line =~ s/(?<![-])psql \(PostgreSQL\)/hg-psql \(HighGo Enterprise Server\)/;
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/test/regress/pg_regress.c";
foreach my $line (@lines)
{
	$line =~ s/%spostgres/%shg-postgres/;
	$line =~ s/%spsql/%shg-psql/;
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/test/regress/pg_regress_main.c";
foreach my $line (@lines)
{
	$line =~ s/%spsql/%shg-psql/;
}
untie @lines;
#------------------------------------------------------------------
tie @lines, "Tie::File", "src/bin/psql/Makefile";
foreach my $line (@lines)
{
	next if($line !~ m/psql/);
	$line =~ s/all: (?<![-])psql/all: hg-psql/;
	$line =~ s/(?<![-])psql:/hg-psql:/;
	$line =~ s/(?<![-])psql\$/hg-psql\$/;
}
untie @lines;
# twice for some omit
tie @lines, "Tie::File", "src/bin/psql/Makefile";
foreach my $line (@lines)
{
	next if($line !~ m/psql/);
	$line =~ s/all: (?<![-])psql/all: hg-psql/;
	$line =~ s/(?<![-])psql:/hg-psql:/;
	$line =~ s/(?<![-])psql\$/hg-psql\$/;
}
untie @lines;
